$import("com.joypard.platform.base.appsystem.AppSystemCompoent");
$import("com.joypard.platform.security.role.RoleGridPanelView");

/**
 * 角色管理-主视图
 */
K.defView("com.joypard.platform.security.role.RoleView", {
	roleCtrl : null,
	searchFormId : "the-role-search3",

	RoleView : function(roleCtrl) {
		this.roleCtrl = roleCtrl;
		this.roleGridPanelView = RoleGridPanelView.instance(this);

		E.addListener("清空用户列表", function() {
			this.userGridPanel.getStore().removeAll();
		}, this);
	},

	render : function() {
		var me = this;
		this.orgTreePanel = this.createOrgTreePanel(); // 左边组织机构树面板

		this.searchRolePanel = this.createSearchRolePanel(); // 搜索面板
		this.roleGridPanel = this.roleGridPanelView.getRoleGridPanel();// 角色列表
		this.userGridPanel = this.createUserGridPanel();// 用户列表

		this.centerPanel = this.createCenterPanel(); // 中间面板
		this.centerPanel.add(this.searchRolePanel);
		this.centerPanel.add(this.roleGridPanel);
		this.centerPanel.add(this.userGridPanel);

		this.roleGridPanel.getStore().on("load", function(store, records, options) {
			var len = records.length;
			if (len == 0) {
				me.userGridPanel.getStore().removeAll();
			} else {
				me.roleGridPanel.getSelectionModel().selectRow(0);
			}
		}, this);

		new K.Ext.MainPanel({
			items : [ this.orgTreePanel, this.centerPanel ]
		});
	},

	/**
	 * 创建组织机构树
	 */
	createOrgTreePanel : function() {
		var me = this, orgTreePanel = AppSystemCompoent.createSelectAppSystemPanel({
			region : "west",
			width : "20%",
			rootVisible : false,
			margins : "2 2 3 2",
			listeners : {
				selectchange : function(node) {
					$("#appSystemIds", me.getForm()).val(node.id);
					$("#appSystemName", me.getForm()).val(node.text);
					me.roleGridPanelView.loadRoles();
				}
			}
		});
		return orgTreePanel;
	},

	/**
	 * 中间面板:包括搜索框，角色列表，用户列表
	 */
	createCenterPanel : function() {
		var options = {
			region : "center",
			margins : "2 2 2 0",
			border : false,
			layout : "border"
		};
		return new K.Ext.Panel(options);
	},

	/**
	 * 搜索面板面板
	 */
	createSearchRolePanel : function() {
		this.setKeypress();
		var options = {
			title : "角色查询",
			iconCls : 'p-search',
			frame : true,
			collapsible : true,
			margins : "0 0 2 0",
			border : true,
			contentEl : this.searchFormId,
			region : "north"
		};
		return new K.Ext.Panel(options);
	},

	/**
	 * 创建用户列表面板
	 */
	createUserGridPanel : function() {
		var options = {
			title : "用户列表",
			iconCls : "p-tab",
			region : 'south',
			margins : "-2 0 0 0",
			height : 180,
			viewConfig : {
				forceFit : true
			},
			split : true,
			border : true,
			store : RoleDI.createUserStore(),
			columns : [ {
				header : "用户编码",
				width : 1,
				sortable : true,
				dataIndex : 'userCode'
			}, {
				id : "userNickName",
				header : "用户名",
				width : 2,
				sortable : true,
				dataIndex : 'userNickName'
			}, {
				header : "账号",
				width : 2,
				sortable : true,
				dataIndex : 'account'
			}, {
				header : "邮箱",
				width : 4,
				sortable : true,
				dataIndex : 'email'
			}, {
				header : "手机",
				width : 3,
				sortable : true,
				dataIndex : 'mobile'
			} ]
		};
		return new K.Ext.grid.GridPanel(options);
	},

	getForm : function() { // cache
		return $("#" + this.searchFormId);
	},

	setKeypress : function() {
		var me = this;
		this.getForm().keypress(function(evt) {
			var tagName = evt.target.tagName;
			if (tagName == "INPUT" && evt.keyCode == Ext.EventObject.ENTER) {
				me.roleGridPanelView.loadRoles();
			}
		});
	}
});
